﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddForm.cs" company="HelloWorld">
//   BY田新宇
// </copyright>
// <summary>
//   添加窗体内的方法
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Form
{
    using System;
    using System.Windows.Forms;

    using WindowsFormsApp2.Service;

    /// <summary>
    /// The add form.
    /// </summary>
    public partial class AddForm : Form
    {
        /// <summary>
        /// The txt_stuId.
        /// </summary>
        private readonly string txtStuId;

        /// <summary>
        /// The txt_stuName.
        /// </summary>
        private readonly string txtStuName = null;

        /// <summary>
        /// The main form.
        /// </summary>
        private readonly Mainform mainForm;

        /// <summary>
        /// Initializes a new instance of the <see cref="AddForm"/> class.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="mainForm">
        /// The main form.
        /// </param>
        public AddForm(string id, Mainform mainForm)
        {
            this.InitializeComponent();
            this.txtStuId = id;
            this.mainForm = mainForm;
        }

        /// <summary>
        /// The button 1_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button1_Click(object sender, EventArgs e)
        {
            // SQLiteConnection con = new SQLiteConnection("Data Source=" + Application.StartupPath + @"\grade.db;");
            // con.Open();
            // string str = "insert into score_info(stuid,semester,numofcourse,nameofcourse,teacher,credit,grade,stuname) values (" + txt_stuid + ",'"
            // + txt_semester.Text + "'," + txt_num.Text + ",'" + txt_name.Text + "','" +
            // txt_teaher.Text + "'," + txt_credit.Value + "," + txt_grade.Text + ",'" + txt_stuname + "')";

            // SQLiteCommand cmd = new SQLiteCommand(str, con);
            // cmd.ExecuteNonQuery();
            // con.Close();
            // con.Dispose();
            var db = new LinkSqLiteDB();   // 创建数据库连接对象
            if (db.DBConn())
            { // 连接数据库
                var str = "insert into score_info(stuid,semester,numofcourse,nameofcourse,teacher,credit,grade,stuname) values (" + this.txtStuId + ",'"
                          + this.txt_semester.Text + "'," + this.txt_num.Text + ",'" + this.txt_name.Text + "','" +
                          this.txt_teaher.Text + "'," + this.txt_credit.Value + "," + this.txt_grade.Text + ",'" + this.txtStuName + "')";
                const string Info = "添加成功！";

                if (db.UpdateDeleteAdd(str))
                {
                    var dr = MessageBox.Show(Info, @"标题", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        this.Visible = false; // 隐藏当前窗口
                        this.mainForm.InitialScore();
                    }
                }
            }

            db.DBClose(); // 关闭数据连接
        }

        /// <summary>
        /// The Add_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void 添加_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false; // 关闭注册窗口的最大化功能
        }
    }
}
